// 使用Uint16Array快速访问并返回偏移量
import {telemetryParser} from "@/utils/byteParser.js";
import frameFormat from "./frame.json"

export function unpackFrame(buffer) {
    const dv = new DataView(buffer);
    const frames = [];
    const FRAME_SIZE = 256;  // 每帧的大小
    const FRAME_START = 0xEB90;
    const FRAME_END = 0x09D7;

    for (let offset = 0; offset + FRAME_SIZE <= dv.byteLength; offset += FRAME_SIZE) {
        if (dv.getUint16(offset) === FRAME_START && dv.getUint16(offset + FRAME_SIZE - 2) === FRAME_END) {
            // 直接使用 Uint8Array 来减少内存开销
            frames.push(buffer.slice(offset, offset + FRAME_SIZE));
        }
    }
    return frames;
}

// 预处理配置为Map
const frameConfigMap = new Map();
Object.entries(frameFormat[0].params[3].enum).forEach(([key, config]) => {
    const typeValue = parseInt(key.replace('0x', ''), 16);
    frameConfigMap.set(typeValue, config);
});

function getFrameConfig(frameType) {
    return frameConfigMap.get(frameType);
}
